# -*- encoding=utf8 -*-

import time
from airtest.core.api import *
from apicore.PocoApi import PocoApi
from base_page.phone_home.Phone_Home import Phone_Home

"""一键上云"""


class OneClickCloudMigration(PocoApi):
    def __init__(self):
        super().__init__()
        self.phone_home = Phone_Home()

    # 进入一键安装
    def enter_page(self):
        self.phone_home.one_click_cloud_migration()

    def everone_click_cloud_migration(self, appliststext):
        self.enter_page()
        time.sleep(2)
        for i in appliststext:
            self.phone_home.poco_click_name_test("com.unicom.cloudphone:id/tv_name", text=i)
        self.phone_home.poco_click(text="一键搬迁")
        time.sleep(9)
        try:
            if(self.phone_home.poco_exists("com.unicom.cloudphone:id/tv_not_allow")==True):
                # 等待图像出现
                zanbukaiqi = wait(
                    Template(self.base_path + "/images/cloudphonehome/" + "暂不开启.png", record_pos=(-0.2, 0.893),
                             resolution=(1080, 2340)), timeout=5, interval=0.5)
                print(zanbukaiqi)
                # 点击找到的图像
                touch(zanbukaiqi)
            if(self.phone_home.poco_exists("com.unicom.cloudphone:id/tv_cancel")==True):
                # 等待图像出现
                buyongle = wait(
                    Template(self.base_path + "/images/cloudphonehome/" + "谢谢,不用了.png", record_pos=(0.219, 0.865),
                             resolution=(1080, 2340)), timeout=5, interval=0.5)
                # 点击找到的图像
                touch(buyongle)
        except Exception as e:
            print(f"应用搬迁失败，错误信息：{e}")
            log(f"应用搬迁失败，错误信息：{e}")
        try:
            # 等待图像出现
            quxiao = wait(
                Template(self.base_path + "/images/cloudphonehome/" + "quxiao.png", record_pos=(0.148, 0.146),
                         resolution=(1080, 2340)), timeout=5, interval=0.5)
            # 点击找到的图像
            touch(quxiao)
        except Exception as e:
            print(f"进入云机失败，错误信息：{e}")
            log(f"进入云机失败，错误信息：{e}")
        self.issucessppinstall(appliststext)
        self.exit_cloudin()

    # 退出云机
    def exit_cloudin(self):  ##退出云机
        self.phone_home.poco_click("com.unicom.cloudphone:id/iv_more")
        self.phone_home.poco_click(text="退出云手机")

    # 向左复位+
    def resetcloudphonein(self):
        for i in range(1, 5):
            swipe((100, 500), (800, 500))

    ##向右边滑动五页寻找指定图片
    def findcloudinpicture(self, picturepath):
        flag = 0
        for i in range(1, 5):
            if not exists(Template(picturepath)):
                swipe((1000, 400), (400, 400))
            else:
                print(f'******已经找到{picturepath}')
                flag = 1
                break
        self.resetcloudphonein()
        return flag

    # 公共方法判断是否安装成功
    def issucessppinstall(self, appnames):  ##公共方法判断是否安装成功
        picture_rootpath = self.base_path + "/images/cloudphonehome/"
        for appname in appnames:
            picturepath = os.path.join(picture_rootpath, f'{appname}.png')
            state = self.findcloudinpicture(picturepath)
            try:
                if state == 0:
                    print(f'{appname}安装失败')
                if state == 1:
                    print(f'{appname}安装成功')
            except Exception as e:
                print(f"安装失败，错误信息：{e}")
            # logger.info(f'{cloudphonename}的{appname}安装失败')
